﻿using System;
using System.IO;
using System.IO.Ports;

namespace serial
{
    class Program
    {
        private SerialPort port;

        private Program(string port_name)
        {
            port = new SerialPort(port_name, 4800, Parity.None, 8, StopBits.One);
            port.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
            OpenPort();
        }

        private void OpenPort()
        {
            // Open the port for communications
            while (true)
            {
                try
                {
                    port.Open();
                    break;
                }
                catch (FileNotFoundException)
                {
                    port.PortName = Program.GetPortName();
                    continue;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }

            }
        }
        private static string GetPortName()
        {
            string[] ports = SerialPort.GetPortNames();
            string portName;
            foreach (string i in ports)
            {
                Console.WriteLine(i);
            }
            while (true)
            {
                try
                {
                    portName = Console.ReadLine();
                    break;
                }
                catch (ArgumentException e1)
                {
                    Console.WriteLine(e1);
                    continue;
                }
            }
            return portName;
        }
        private  void SendSampleData(byte[] data)
        {
            port.Write(new byte[] { 0x01 }, 0, 1);
            foreach (byte s in data) {
                try
                {
                    port.Write(new byte[] { s }, 0, 1);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }

        private static void DataReceivedHandler(
                       object sender,
                       SerialDataReceivedEventArgs e)
        {
            SerialPort sp = (SerialPort)sender;
            Byte indata = Convert.ToByte(sp.ReadByte());
            Console.WriteLine("Data Received:");
            Console.WriteLine(BitConverter.ToString(new Byte[] { indata }));
        }


        static void Main(string[] args)
        {
            string path;
            if (args.Length == 1)
            {
                path = args[0];
            }
            else
            {
                path = "E:\\Csharp\\serial\\__code.bin";
            }
            string[] ports = SerialPort.GetPortNames();
            foreach(string i in ports)
            {
                Console.WriteLine(i);
            }
            string portName = ports[0]
                ;
            Program pro = new Program(portName);
            //string port_name =  Console.ReadLine();
            ReadFile file = new ReadFile(path);
            byte[] data = file.GetBytes();
            pro.SendSampleData(data);
            Console.ReadLine();
            pro.port.Close();

        }
    }
}
